/**
 * @desc 手机号
 */
<template>
    <view class="phone">
        <nut-noticebar v-if="id && stop" :text="id && !stop ? '审核通过,你已经是教练了。' : `审核中请稍后.....`"
            :background="`rgba(251, 248, 220, 1)`" :color="`#D9500B`">
        </nut-noticebar>
        <nut-input label="姓名" placeholder="姓名" v-model="coach" />
        <view class="phone-btn">
            <nut-button type="primary" @click="onGetPhoneNumber">确认提交</nut-button>
            <nut-button v-if="id && stop" type="primary" @click="stopFunc" :style="{ marginLeft: '10px' }">撤销申请
            </nut-button>
        </view>
        <nut-dialog title="提示" :content="content" v-model:visible="visible" @cancel="onCancel" @ok="onOk" />
    </view>
</template>

<script>
import db from '../../server/index';
import Taro from '@tarojs/taro';
const openid = Taro.getStorageSync('openid')
export default {
    coach: 'Logs',
    data() {
        return {
            visible: false,
            content: '是否确认提交修改!',
            coach: '',
            id: '',
            stop: false
        }
    },
    created() {
        this.init()
    },
    onPullDownRefresh() {
        Taro.showLoading({
            title: '加载中',
        })
        this.init()
    },
    methods: {
        stopFunc() {
            this.content = '是否确认撤销申请!',
                this.visible = true
        },
        onCancel() {
            this.visible = false
        },
        onGetPhoneNumber() {
            this.content = '是否确认提交修改!',
                this.visible = true
        },
        init() {
            db.collection('coach').where({
                openid
            }).get().then(({ data = [] }) => {
                if (data.length) {
                    this.coach = data[0].coach
                    this.id = data[0]._id
                    this.stop = data[0].stop
                } else {
                    this.coach = ''
                    this.id = ''
                    this.stop = false
                }
                Taro.stopPullDownRefresh();
                Taro.hideLoading();
            })
        },
        onOk() {
            if (this.content === '是否确认撤销申请!') {
                db.collection('coach').where({
                    _id: this.id
                }).remove().then(() => {
                    Taro.showToast({
                        title: '撤销申请成功！',
                        icon: 'success',
                        duration: 2000,
                        mask: true
                    })
                    this.init();
                })
                return
            }
            if (this.id) {
                db.collection('coach').where({
                    _id: this.id
                }).update({
                    data: {
                        coach: this.coach,
                        stop: this.stop
                    }
                }).then(() => {
                    Taro.showToast({
                        title: '修改成功！',
                        icon: 'success',
                        duration: 2000,
                        mask: true
                    })
                    this.init()
                })
            } else {
                const { avatarUrl, nickName } = Taro.getStorageSync('userInfo');
                db.collection('coach').add({
                    data: {
                        coach: this.coach,
                        openid,
                        stop: true,
                        avatarUrl,
                        nickName
                    }
                }).then(() => {
                    Taro.showToast({
                        title: '新增成功！',
                        icon: 'success',
                        duration: 2000,
                        mask: true
                    })
                    this.init()
                })
            }
        }
    }
}
</script>

<style lang="less">
.phone {
    &-btn {
        text-align: center;
        margin-top: 10px;
    }
}
</style>
